package com.vay.de.dang.online.phone;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.vay.de.dang.online.entiy.InstallAppEntity;

public class InstallAppHelper {

    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static List<InstallAppEntity> queryInstallApp(PackageManager packageManager) {
        List<InstallAppEntity> installApps = new ArrayList();
        InstallAppEntity appInfoBean;
        String appName;
        String packageName;
        String versionName;
        //String installTime;
        //String lastUpdateTime;
        try {
            List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
            if (packageInfoList == null || packageInfoList.size() == 0) {
                return installApps;
            }

            for (int i = 0; i < packageInfoList.size(); i++) {
                PackageInfo packageInfo = packageInfoList.get(i);

                // 过滤掉系统app
                if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
                    continue;
                }

                appName = packageInfo.applicationInfo.loadLabel(packageManager).toString();
                packageName = packageInfo.packageName;
                versionName = packageInfo.versionName;
                if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
                    continue;
                }
                //installTime = df.format(packageInfo.firstInstallTime);
                //lastUpdateTime = df.format(packageInfo.lastUpdateTime);

                appInfoBean = new InstallAppEntity();
                appInfoBean.setAppName(appName);
                appInfoBean.setAppPackageName(packageName);
                appInfoBean.setFirstInstallTime(packageInfo.firstInstallTime);
                appInfoBean.setLastUpdateTime(packageInfo.lastUpdateTime);
                appInfoBean.setAppVersion(versionName);
                installApps.add(appInfoBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return installApps;
    }

    public static Boolean filterApp(ApplicationInfo info) {
        //原来是系统应用，用户手动升级
        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
            return true;
        }

        //用户自己安装的应用程序
        return (info.flags & ApplicationInfo.FLAG_SYSTEM) == 0;
    }

    public static void installApk(Context context, String authorities, File apk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, authorities, apk);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(apk);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}

